package com.example.validproject.protocol;


import java.util.ArrayList;
import java.util.HashMap;
import com.example.validproject.exception.BusinessException;
import lombok.Builder;
import lombok.Data;

/**
 * @author kh
 * Created by on  2021-09-27 10:48
 */
@Data
@Builder
public class Result<T> {

    private String message;

    /**
     * 返回消息类型,用于复杂返回信息,0代表普通返回信息,1代表特殊返回信息
     */
    private Integer code;

    private T data;

    private String bussinessCode;


    public static Result of(boolean success) {
        return Result.builder().code(Integer.valueOf(BaseErrorCode.SUCCESS.getCode()))
                .message(BaseErrorCode.SUCCESS.getMessage()).data(new ArrayList<>()).build();
    }

    public static Result success() {
        return Result.builder().code(Integer.valueOf(BaseErrorCode.SUCCESS.getCode()))
                .message(BaseErrorCode.SUCCESS.getMessage()).data(new ArrayList<>()).build();
    }

    public boolean isSuccess() {
        return BaseErrorCode.SUCCESS.getCode().equals(this.code);
    }

    public static Result success(Object data) {
        ResultBuilder<Object> builder = Result.<Object>builder()
                .code(Integer.valueOf(BaseErrorCode.SUCCESS.getCode())).message(BaseErrorCode.SUCCESS.getMessage());
        /*if (data instanceof Page) {
            Page page = (Page) data;
            HashMap<String, Object> map = new HashMap<>(2);
            map.put("total", page.getTotal());
            map.put("records", page.getRecords());
            builder.data(map);
        } else {
            builder.data(data);
        }*/
        builder.data(data);
        return builder.build();
    }

    public static Result successWithMessage(Object data, String message) {
        ResultBuilder<Object> builder = Result.<Object>builder()
                .code(Integer.valueOf(BaseErrorCode.SUCCESS.getCode())).message(message);
        /*if (data instanceof Page) {
            Page page = (Page) data;
            HashMap<String, Object> map = new HashMap<>(2);
            map.put("total", page.getTotal());
            map.put("records", page.getRecords());
            builder.data(map);
        } else {
            builder.data(data);
        }*/
        return builder.build();
    }

    public static Result successMessage(String message) {
        return Result.builder().code(Integer.valueOf(BaseErrorCode.SUCCESS.getCode())).data(new ArrayList<>()).message(message).build();
    }

    public static Result failure(String msg) {
        return failure(BaseErrorCode.INTERNAL_SERVER_ERROR, msg);
    }

    public static Result failure(IErrorCode errorCode) {
        return Result.builder().code(Integer.valueOf(BaseErrorCode.INTERNAL_BUSSINESS_ERROR.getCode())).message(errorCode.getMessage()).data(new ArrayList<>()).bussinessCode(errorCode.getCode()).build();
    }

    public static Result failure(IErrorCode errorCode, String msg) {
        return Result.builder().code(Integer.valueOf(BaseErrorCode.INTERNAL_BUSSINESS_ERROR.getCode()))
                .message(msg == null ? errorCode.getMessage() : msg).data(new ArrayList<>()).bussinessCode(errorCode.getCode()).build();
    }

    public static Result failure(BusinessException exception) {
        return failure(exception.getErrorCode(), exception.getMessage());
    }

}

